package com.shopping.dao;

import com.shopping.pojo.Appraise;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Mapper
public interface AppraiseDao {

    String TABLE_NAME = "appraise";
    String INSERT_FIELDS = "trade_id, user_id, store_id, is_anonymity, appraise_content, " +
            "trade_grade, store_grade, transport_grade, pic_1, pic_2, pic_3";
    String SELECT_FIELDS = "id, trade_id tradeId, user_id userId, store_id storeId, is_anonymity isAnonymity, appraise_content appraiseContent, " +
            "trade_grade tradeGrade, store_grade storeGrade, transport_grade transportGrade, pic_1 pic1, pic_2 pic2, pic_3 pic3";

    @Insert({"insert into ", TABLE_NAME, "(", INSERT_FIELDS, ") values(#{tradeId}, #{userId}, #{storeId}, #{isAnonymity}, #{appraiseContent}, #{tradeGrade}, " +
            "#{storeGrade}, #{transportGrade}, #{pic1}, #{pic2}, #{pic3})"})
    int insertAppraise(Appraise appraise);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where trade_id=#{tradeId} and store_id=#{storeId}"})
    List<Appraise> selectAllAppraise(@Param("tradeId") int tradeId, @Param("storeId") int storeId);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where trade_id=#{tradeId} and store_id=#{storeId} and user_id=#{userId}"})
    Appraise selectUserAppraiseToTrade(@Param("tradeId") int tradeId, @Param("storeId") int storeId, @Param("userId") int userId);

    @Select({"select last_insert_id()"})
    Integer lastInsertId();

}
